package com.hyk.commonLib.common.activity;

import android.os.Bundle;

import androidx.annotation.CallSuper;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import androidx.databinding.ViewDataBinding;

import com.dylanc.activityresult.launcher.StartActivityLauncher;
import com.dylanc.viewbinding.base.ViewBindingUtil;
import com.hjq.permissions.XXPermissions;
import com.hyk.commonLib.common.entity.ReqPermissionResult;
import com.hyk.commonLib.common.utils.BusUtils;
import com.trello.rxlifecycle4.LifecycleTransformer;
import com.trello.rxlifecycle4.android.ActivityEvent;
import com.trello.rxlifecycle4.components.support.RxAppCompatActivity;

import io.reactivex.rxjava3.core.Observable;
import lombok.Getter;

/**
 * Created by 贺玉琨 on 2019/3/5 0005.
 */
public abstract class BaseActivity<DATA_BINDING extends ViewDataBinding> extends RxAppCompatActivity {
    public final StartActivityLauncher startActivityLauncher = new StartActivityLauncher(this);
    @Getter
    protected DATA_BINDING dataBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataBinding = ViewBindingUtil.inflateWithGeneric(this, getLayoutInflater());
        setContentView(dataBinding.getRoot());
        BusUtils.register(this);
    }

    @CallSuper
    @Override
    protected void onDestroy() {
        BusUtils.unregister(this);
        super.onDestroy();
    }

//    @Override
//    protected void attachBaseContext(Context newBase) {
//        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
//    }

    public <T> LifecycleTransformer<T> bindUntilDestroy() {
        return bindUntilEvent(ActivityEvent.DESTROY);
    }

    public Observable<ReqPermissionResult> requestPermissions(String... permissions) {
        return Observable.create(emitter -> XXPermissions.with(BaseActivity.this)
                .permission(permissions)
                .request((permissions1, all) -> {
                    ReqPermissionResult reqPermissionResult;
                    if (all) {
                        reqPermissionResult = ReqPermissionResult.success(permissions1);
                    } else {
                        boolean isPermanentDenied = XXPermissions.isPermanentDenied(BaseActivity.this, permissions1);
                        reqPermissionResult = ReqPermissionResult.fail(permissions1, isPermanentDenied);
                    }
                    emitter.onNext(reqPermissionResult);
                }));
    }
//
//    /**
//     * 设置为沉浸式
//     */
//    protected void setImmersiveEnable() {
//        Window window = getWindow();
//        View decorView = window.getDecorView();
//        int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
//        decorView.setSystemUiVisibility(option);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//            window.setStatusBarColor(Color.TRANSPARENT);
//        }
//    }
//
//    /**
//     * 取消为沉浸式
//     */
//    protected void setImmersiveDisable() {
//        Window window = getWindow();
//        View decorView = window.getDecorView();
//        int option = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
//        decorView.setSystemUiVisibility(option);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//        }
//    }

    /**
     * 状态栏文字设为黑色
     */
    protected void lightStatusBar() {
        WindowInsetsControllerCompat windowInsetsController = ViewCompat.getWindowInsetsController(getWindow().getDecorView());
        if (windowInsetsController != null) {
            windowInsetsController.setAppearanceLightStatusBars(true);
        }
    }

    /**
     * 状态栏文字设为白色
     */
    protected void dimStatusBar() {
        WindowInsetsControllerCompat windowInsetsController = ViewCompat.getWindowInsetsController(getWindow().getDecorView());
        if (windowInsetsController != null) {
            windowInsetsController.setAppearanceLightStatusBars(false);
        }
    }
}
